﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingWorkshop.DALEF;
using System.Data;
using System.Configuration;

namespace TrainingWorkshop.BLL
{
    public class Logical
    {
        public List<TrainingWorkshop.Model.Teacher> Re;

        public List<TrainingWorkshop.Model.Department> Dep;

        public List<TrainingWorkshop.Model.Teacher> Tea;

        public SqlFactory SqlFactoryInstance;

        public Logical()
        {

            Re = new List<TrainingWorkshop.Model.Teacher>();// for showing
            Dep = new List<TrainingWorkshop.Model.Department>();// for showing
            Tea = new List<TrainingWorkshop.Model.Teacher>();//for operation
            //SqlFactoryInstance = new SqlFactory(ConfigurationManager.ConnectionStrings["Database"].ConnectionString); //for ado.net
            SqlFactoryInstance = new SqlFactory(ConfigurationManager.ConnectionStrings["projectbEntities"].ConnectionString); //for entity framework
            
        }


        public bool OperationChoose(string operation, bool all, TrainingWorkshop.Model.Teacher Tea = null)
        {
            
            Re.Clear();
 
            if(operation.Equals("Select"))//call select for gridview
            {
                
                try
                {
                    if (all)
                    {
                      List<Teacher> tea= SqlFactoryInstance.CreateDepartmentAndTeacherRepository().SelectAllFromDepartmentAndTeacher();
                       Re = ModelTranslator.TeacherEFtoTeacher(tea);

                    }
                    else
                    {
                        List<TrainingWorkshop.Model.Teacher> teamodel = new List<Model.Teacher>();
                        teamodel.Add(Tea);
                        //List<Teacher> tea = SqlFactoryInstance.CreateDepartmentAndTeacherRepository().SelectFromDepartmentAndTeacher();
                        //Re = ModelTranslator.TeacherEFtoTeacher(tea);
                        
                    }

                    

                    return true;
                }

                catch (Exception)
                {
                    return false;
                }

               
            }
            else if (operation.Equals("Update"))//call update for Teacher Table
            {
                try
                {
                    List<TrainingWorkshop.Model.Teacher> teamodel = new List<Model.Teacher>();
                    teamodel.Add(Tea);
                    List<Teacher> teas = ModelTranslator.TeachertoTeacherEF(teamodel);
                    foreach (var a in teas)
                    {
                        SqlFactoryInstance.CreateTeacherRepository().Update(a);
                    }
                    return true;
                }

                catch (Exception)
                {
                    return false;
                }
            }
            else if (operation.Equals("Insert"))//call insert for Teacher Table
            {
                try
                {
                    List<TrainingWorkshop.Model.Teacher> teas = new List<Model.Teacher>();
                    teas.Add(Tea);
                    List<Teacher> temp = ModelTranslator.TeachertoTeacherEF(teas);
                    foreach (var a in temp)
                    {
                        SqlFactoryInstance.CreateTeacherRepository().Create(a);

                    }
                    return true;
                }

                catch (Exception)
                {
                    return false;
                }
            }
            else if (operation.Equals("Delete"))//call delete for Teacher Table
            {
                try
                {
                    List<TrainingWorkshop.Model.Teacher> teas = new List<Model.Teacher>();
                    teas.Add(Tea);
                    List<Teacher> temp = ModelTranslator.TeachertoTeacherEF(teas);
                    foreach (var a in temp)
                    {

                        SqlFactoryInstance.CreateTeacherRepository().Delete(a);
                    }
                    return true;
                }

                catch (Exception ex)
                {
                    return false;
                }
            }

            return false;
        }

        public bool ChooseTable(string TableName,  bool all, int PrimaryKey = 0)
        {

            
            try
            {
                if (TableName.Equals("Department"))
                {

                    if (all)
                    {
                        List<Department> d = SqlFactoryInstance.CreateDepartmentRepository().SelectAll();
                        Dep = ModelTranslator.DepartmentEFtoDepartment(d);
                        
                    }

                    else
                    {

                        Department a = new Department()
                        {
                            DepartmentId=PrimaryKey
                        };

                        List<Department> d = SqlFactoryInstance.CreateDepartmentRepository().Select(a);
                        Dep = ModelTranslator.DepartmentEFtoDepartment(d);

                    }

                    return true;

                }

                else if (TableName.Equals("Teacher"))
                {
                    if (all)
                    {
                        List<Teacher> d = SqlFactoryInstance.CreateTeacherRepository().SelectAll();
                        Tea = ModelTranslator.TeacherEFtoTeacher(d);
                        
                    }

                    else
                    {

                        Teacher a = new Teacher() 
                        {
                            TeacherId=PrimaryKey
                        };
                        List<Teacher> d = SqlFactoryInstance.CreateTeacherRepository().Select(a);
                        Tea = ModelTranslator.TeacherEFtoTeacher(d);
                       

                    }

                    return true;

                }
                else
                {
                     return false;
                }

            }

            catch (Exception)
            {
                return false;
            }

        }
 
        
    }
}
